package cn.edu.sdjzu.xg23.kcsj01_backend.service;


import cn.edu.sdjzu.xg23.kcsj01_backend.entity.EmployeeAssPaper;
import cn.edu.sdjzu.xg23.kcsj01_backend.entity.JournalImpact;
import cn.edu.sdjzu.xg23.kcsj01_backend.entity.LiteratureDatabaseAsPaper;
import cn.edu.sdjzu.xg23.kcsj01_backend.entity.Paper;
import cn.edu.sdjzu.xg23.kcsj01_backend.mapper.AbsAchievementMapper;
import cn.edu.sdjzu.xg23.kcsj01_backend.mapper.EmployeeAssPaperService;
import cn.edu.sdjzu.xg23.kcsj01_backend.mapper.PaperMapper;
import cn.edu.sdjzu.xg23.kcsj01_backend.service.impl.EmployeServiceImpl;
import cn.edu.sdjzu.xg23.kcsj01_backend.service.impl.SubjectServiceImpl;
import cn.edu.sdjzu.xg23.kcsj01_backend.utils.SnowflakeIdGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import java.util.List;

@Service
public class PaperService {
    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;
    @Autowired
    private AbsAchievementMapper absAchievementMapper;
     @Autowired
     private SubjectServiceImpl subjectService;
     @Autowired
     private EmployeServiceImpl employeeService;
     @Autowired
     private EmployeeAssPaperService employeeAssPaperService;
     @Autowired
     private LiteratureDatabaseService literatureDatabaseService;
     @Autowired
     private LiteratureDatabaseAsPaperService  literatureDatabaseAsPaperService;
     @Autowired
      private JournalService journalService;
     @Autowired
     private JournalImpactService journalImpactService;

    public PageInfo<Paper> get(int pageNum, int pageSize, Paper paper){
        PageHelper.startPage(pageNum,pageSize);
        List<Paper> papersList  = paperMapper.get(paper);
        return PageInfo.of(papersList);
    }
    @Transactional
    public String save(Paper paper){
        Long id = snowflakeIdGenerator.generateId();
        absAchievementMapper.addAchievement(id,"论文");
        paper.setId(id);
        JournalImpact journalImpact=paper.getJournal().getJournalImpact();
        journalImpact.setJournal(paper.getJournal());
        journalImpactService.save(journalImpact);
        List <EmployeeAssPaper> otherAuthors = paper.getOtherAuthors();

        if( otherAuthors!=null){
            for(EmployeeAssPaper employee : otherAuthors){
                employee.setPaper(paper);
            }
        }

        List<LiteratureDatabaseAsPaper> literatureDatabases = paper.getLiteratureDatabases();
        if( literatureDatabases!=null){
            for(LiteratureDatabaseAsPaper literatureDatabase : literatureDatabases){
                if( literatureDatabase.getId() == null){
                    literatureDatabase.setPaper(paper);
                }

            }
        }

        if(paper.getJournal().getId()== null){
            journalService.save(paper.getJournal());
        }
        boolean isAdd = paperMapper.save(paper);
        if(isAdd){
            if(otherAuthors!=null){
               for(EmployeeAssPaper employee : otherAuthors){
                  employeeAssPaperService.save(employee);
               }

            }
            if(literatureDatabases!=null){
                for(LiteratureDatabaseAsPaper literatureDatabase : literatureDatabases){
                    literatureDatabaseAsPaperService.save(literatureDatabase);
                }
            }

            return "保存成功";
        }
        return "保存失败";
    }
    public String update(Paper paper){
        List<EmployeeAssPaper> employeeAssPapers= paper.getOtherAuthors();
        if (employeeAssPapers!=null){
            for(EmployeeAssPaper employee : employeeAssPapers){
                employee.setPaper(paper);
                employeeAssPaperService.update(employee);
            }
        }
        JournalImpact journalImpact=paper.getJournal().getJournalImpact();
        journalImpact.setJournal(paper.getJournal());
        journalImpactService.update(journalImpact);
        if(paperMapper.update(paper)){
            return "修改成功";
        }
        System.out.println("修改失败");
        return "修改失败";
    }
    public String delete(long id){
        if(paperMapper.delete(id)){
            return "删除成功";
        }
        return "删除失败";
    }
    public boolean approvePaper(Paper paper) {
        return paperMapper.approvePaper(paper);
    }
}
